﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tesseract;

namespace Tools.OCR.Tesseract
{

    public enum OCRLanguage
    {
        eng,
        chi_sim
    }

    public class TesseractOCRHelper
    {

        public static string GetProductNumberFromImage(string imagePath)
        {
            var testpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tessdata");
            using (var ocr = new TesseractEngine(testpath, "chi_sim", EngineMode.Default))
            {
                var pix = PixConverter.ToPix(new Bitmap(imagePath));
                using (var page = ocr.Process(pix))
                {
                    string text = page.GetText();
                    return text;
                }
            }
            return null;
        }

        public static string GetProductNumberFromImage(Bitmap image, OCRLanguage language = OCRLanguage.eng)
        {
            string ready_orc_word = "0123456789";
            var testpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tessdata");
            using (var ocr = new TesseractEngine(testpath, language.ToString(), EngineMode.TesseractAndLstm))
            {
                ocr.SetVariable("tessedit_char_whitelist", ready_orc_word); // If digit only
                var pix = PixConverter.ToPix(image);
                using (var page = ocr.Process(pix))
                {
                    string text = page.GetText();
                    return text;
                }
            }
            return null;
        }

    }
}
